<?php
/**
 * Soar Framework - Lightweight and simple to use PHP Framework
 *
 * Soar is free software and is distributed WITHOUT ANY WARRANTY
 *
 * @package Soar
 * @version 1.0 $Id: SoarController.php 2 2012-04-06 21:50:33Z shay@shayanderson.com $
 * @copyright Copyright (c) 2012 ShayAnderson.com
 * @license http://www.gnu.org/licenses/gpl.html GPL License
 * @link http://www.shayanderson.com/projects/soar-php-framework.htm
 */

/**
 * Soar Framework Abstract Core Controller
 *
 * @package Soar
 * @name SoarController
 * @author Shay Anderson 03.12
 *
 * @example
 * class ExampleController extends SoarController
 *	{
 *		public function action()
 *		{
 *			// current action, ex: 'example-action'
 *			echo $this->action . '<br />';
 *			// action name, ex: 'exampleAction'
 *			echo $this->action_name . '<br />';
 *			// access configuration setting
 *			echo $this->conf->app->myconf . '<br />';
 *			// current controller, ex: 'index'
 *			echo $this->controller . '<br />';
 *			// controller name, ex: 'IndexController'
 *			echo $this->controller_name . '<br />';
 *			// GET param 'var1'
 *			echo $this->get->var1 . '<br />';
 *			// current module, ex: 'mod1/modsub'
 *			echo $this->module . '<br />';
 *			// module parts, ex: ('mod1', 'modsub')
 *			print_r($this->module_parts);
 *			// POST param 'field1'
 *			echo $this->post->field1 . '<br />';
 *			// registry param
 *			echo $this->registry->var1 . '<br />';
 *			// SERVER param 'SERVER_NAME'
 *			echo $this->server->SERVER_NAME . '<br />';
 *			// SESSION param 'sess_var1'
 *			echo $this->session->sess_var1 . '<br />';
 *			// set view vars
 *			$this->view->set('var1', 'my value');
 *		}
 *	}
 */
abstract class SoarController
{
	/**
	 * Soar action
	 *
	 * @var string
	 */
	protected $action;

	/**
	 * Soar action name, ex: someAction
	 *
	 * @var string
	 */
	protected $action_name;

	/**
	 * Config object
	 *
	 * @var SoarConf
	 */
	protected $conf;

	/**
	 * Soar controller
	 *
	 * @var string
	 */
	protected $controller;

	/**
	 * Soar controller name, ex: IndexController
	 *
	 * @var string
	 */
	protected $controller_name;

	/**
	 * Get request
	 *
	 * @var SoarRequest
	 */
	protected $get;

	/**
	 * Soar module, ex: mod1/modsub
	 *
	 * @var string
	 */
	protected $module;

	/**
	 * Soar module parts, ex: ('mod1', 'modsub' [, ...])
	 *
	 * @var array
	 */
	protected $module_parts = array();

	/**
	 * Post request
	 *
	 * @var SoarRequest
	 */
	protected $post;

	/**
	 * Registry object
	 *
	 * @var SoarRegistry
	 */
	protected $registry;

	/**
	 * Server request
	 *
	 * @var SoarRequest
	 */
	protected $server;

	/**
	 * Session request
	 *
	 * @var SoarRequest
	 */
	protected $session;

	/**
	 * Template path
	 *
	 * @var string
	 */
	protected $template_path;

	/**
	 * View object
	 *
	 * @var SoarView
	 */
	protected $view;

	/**
	 * Init
	 *
	 * @param SoarView $view
	 * @param SoarRegistry $registry
	 */
	final public function __construct(SoarView &$view, SoarRegistry &$registry)
	{
		Soar::log(__CLASS__ . ': initialized', SoarDebugger::LEVEL_DEBUG);
		// set params for child controller
		$this->conf = &SoarConf::getInstance();
		$this->view = &$view;
		$this->registry = &$registry;

		// set Soar router params
		$this->soar = new SoarRegistry;
		$router = &SoarRouter::getInstance();
		$this->action = $router->getAction();
		$this->action_name = $router->getActionName();
		$this->controller = $router->getController();
		$this->controller_name = $router->getControllerName();
		$this->module = $router->getModule();
		$this->module_parts = $router->getModuleParts();
		$this->template_path = $router->getTemplatePath();

		// set Soar requests
		$this->get = &SoarRequest::getInstance(SoarRequest::TYPE_GET);
		$this->post = &SoarRequest::getInstance(SoarRequest::TYPE_POST);
		$this->server = &SoarRequest::getInstance(SoarRequest::TYPE_SERVER);
		$this->session = &SoarRequest::getInstance(SoarRequest::TYPE_SESSION);

		// run Controller constructor method
		$this->__init();

		// run child constructor method
		$this->__main();
	}

	/**
	 * Controller constructor method
	 */
	abstract public function __init();

	/**
	 * Child constructor method
	 */
	public function __main()
	{
		// overridable
	}

	/**
	 * Default action
	 */
	abstract public function action();

	/**
	 * Finalize controller
	 */
	abstract public function finalize();
}
?>